﻿using System.Threading;

namespace FotoAlbum.Async
{
    /// <summary>
    /// The counter is used to display the progress on the progressbar.
    /// </summary>
    internal static class Counter
    {
        private static int _current = -1;
        private static int _maximum = -1;
        private static int _abort = -1;

        /// <summary>
        /// The current amount of images that have been loaded.
        /// </summary>
        public static int Current { get { return _current + 1; } }

        /// <summary>
        /// The maximum amount of images that have to be loaded.
        /// </summary>
        public static int Maximum { get { return _maximum + 1; } }

        /// <summary>
        /// Stop loading new images from the current index.
        /// </summary>
        public static int AbortAt { get { return _abort; } }

        public static int Start()
        {
            return Interlocked.Increment(ref _maximum);
        }

        /// <summary>
        /// Call this when a image has been loaded.
        /// </summary>
        /// <returns>true when the maximum amount of tasks has completed; false otherwise.</returns>
        public static bool Completed()
        {
            Interlocked.Increment(ref _current);
            return (_current == _maximum);
        }

        ///// <summary>
        ///// Reset the counter.
        ///// </summary>
        //public static void Reset()
        //{
        //    _current = -1;
        //    _maximum = -1;
        //}

        /// <summary>
        /// Abort loading images at the specified index.
        /// </summary>
        public static void Abort()
        {
            _abort = _maximum;
        }
    }
}
